package com.gxl.butt.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.gxl.butt.DAO.IbtipDAO;
import com.gxl.butt.domain.BT_Ip;
import com.gxl.butt.dto.bo.Ip;
import com.gxl.butt.exception.CException;
import com.gxl.butt.factory.ConfigFactory;
import com.gxl.butt.service.IIPService;
import com.gxl.butt.dto.vo.IPBean;
import com.gxl.butt.utils.DateUtil;
import com.gxl.butt.utils.JSONUtils;
import com.gxl.butt.utils.PageFinder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

import static com.gxl.butt.value.ErrorMsg.*;

/**
 * Created by GUOXIAOLIN on 2018/7/16.
 */
@Service(interfaceClass = IIPService.class,retries = 0)
@Component
public class IPServiceImpl implements IIPService {

    private Logger log = LoggerFactory.getLogger(KafkaOperatorServiceImpl.class);
    @Autowired
    private IbtipDAO ibtipDAO;

    @Override
    @Transactional
    public JSONObject addIp(String ip,Long userid) {

        BT_Ip bt_ip = ConfigFactory.getIpInstance(ip, userid, " ");
        Long btipId = ibtipDAO.saveBtIp(bt_ip);
        Ip ipBean = new Ip();
        ipBean.setId(btipId);
        return JSONUtils.getRoot(0, SUCCESS_MSG, ipBean);
    }

    @Override
    @Transactional
    public JSONObject delIpById(Long id){
       boolean flag =  false;
       flag = ibtipDAO.delIpById(id);
       if (flag){
           return JSONUtils.getRoot(0, SUCCESS_MSG, STR_IS_EMPTY);
       }
        return JSONUtils.getRoot(-1, DEL_IP_LOST, STR_IS_EMPTY);
    }

    @Override
    @Transactional
    public JSONObject getIpList(Long userid) {
        List<BT_Ip> bt_ipList = ibtipDAO.getIpListByUserId(userid);
        return JSONUtils.getRoot(0, SUCCESS_MSG, bt_ipList);
    }

    @Override
    @Transactional
    public JSONObject getListByIpAndUserId(String ip,Long userid) {
        List<BT_Ip> bt_ips = ibtipDAO.getListByIpAndUserId(ip,userid);
        if (bt_ips != null && bt_ips.size() > 0){
            return  JSONUtils.getRoot(0, SUCCESS_MSG, STR_IS_EMPTY);
        }
        return  JSONUtils.getRoot(1, FAILD_MSG, STR_IS_EMPTY);
    }

    @Override
    @Transactional
    public JSONObject getListByIpAndopenid(String ip,String openid) {
        List<BT_Ip> bt_ips = ibtipDAO.getListByIpAndOpenid(ip,openid);
        if (bt_ips != null && bt_ips.size() > 0){
            return  JSONUtils.getRoot(0, SUCCESS_MSG, STR_IS_EMPTY);
        }
        return  JSONUtils.getRoot(1, FAILD_MSG, STR_IS_EMPTY);
    }

    @Override
    @Transactional
    public JSONObject getipsByOpenid(Integer pageNo,Integer pageSize,String openid) {
        List<BT_Ip> bt_ipList = ibtipDAO.getipsByOpenid(openid);
        if(bt_ipList == null || bt_ipList.size() <= 0){
            return JSONUtils.getRoot(0,SUCCESS_MSG,"");
        }
        PageFinder pf = new PageFinder(pageNo,pageSize, bt_ipList.size(),  bt_ipList);
        Object ddo = pf.getResult();
//        log.info(JSONObject.toJSON(ddo).toString());
        JSONArray jsonArray = JSONArray.parseArray(JSONObject.toJSON(ddo).toString());
        JSONArray resdatas = new JSONArray();
        Integer sp = pf.getStartOfPage();
        for (int i = sp ;i<jsonArray.size();i++){
            JSONObject jsonObject = new JSONObject();
//            log.info(String.valueOf(jsonArray.getJSONArray(i).get(0)));
            jsonObject.put("id",String.valueOf(jsonArray.getJSONArray(i).get(0)));
            jsonObject.put("ip",String.valueOf(jsonArray.getJSONArray(i).get(1)));
            resdatas.add(jsonObject);
            if (resdatas.size() == pageSize){
                break;
            }
        }
        return JSONUtils.getRoot(0, SUCCESS_MSG, resdatas);
    }

    @Override
    @Transactional
    public JSONObject addIpWithOpenid(String ip, String openid) {
        String[] ips = ip.split(",");
        for (int i =0 ;i<ips.length;i++){
            BT_Ip bt_ip = ConfigFactory.getIpInstance(ips[i], 0l, openid);
            Long btipId = ibtipDAO.saveBtIp(bt_ip);
            bt_ip = null;
        }
        return JSONUtils.getRoot(0, SUCCESS_MSG, STR_IS_EMPTY);
    }
}
